<template>
  <view>
    <!-- 搜索框 -->
    <view class="search-box">
      <uni-search-bar @input="antiShake" placeholder="请输入搜索内容" :radius="100" cancelButton="none"></uni-search-bar>
    </view>
    <!-- 搜索列表 -->
    <view class="sugg-list" v-if="searchResult.length !== 0">
      <view class="sugg-item" v-for="(item, i) in searchResult" :key="i" @click="gotoDetail(item)">
        <view class="goods-name">
          {{item.goods_name}}
        </view>
        <uni-icons type="arrow-right" size="17"></uni-icons>
      </view>
    </view>
    <!-- 搜索历史 -->
    <view class="history-box" v-else>
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17" @click="clean"></uni-icons>
      </view>
      <view class="history-list">
        <uni-tag :text="item" v-for="(item, i) in histories" :key="i" @click="gotoGoodsList(item)"></uni-tag>
      </view>

    </view>
  </view>
</template>

<script>
  import {
    debounce
  } from '@/utils/tools.js'
  import {
    getSearchList
  } from '@/utils/api.js'
  export default {
    data() {
      return {
        kw: '',
        searchResult: [],
        historyList: []
      };
    },
    methods: {
      antiShake: debounce(function(...args) {
        this.input(...args), 500
      }),
      input(e) {
        this.kw = e
        this.getSearchListEx()
      },
      async getSearchListEx() {
        if (typeof(this.kw) === "undefined" || this.kw.length === 0) {
          this.searchResult = []
          return
        }
        const {
          data: res
        } = await getSearchList({
          data: {
            query: this.kw
          }
        })
        if (res.meta.status !== 200) {
          this.$showMsg()
          return
        }
        this.searchResult = res.message
        this.saveSearchHistory()
      },
      gotoDetail(e) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + e.goods_id
        })
      },
      saveSearchHistory() {
        // 1. 将 Array 数组转化为 Set 对象
        const set = new Set(this.historyList)
        // 2. 调用 Set 对象的 delete 方法，移除对应的元素
        set.delete(this.kw)
        // 3. 调用 Set 对象的 add 方法，向 Set 中添加元素
        set.add(this.kw)
        // 4. 将 Set 对象转化为 Array 数组
        this.historyList = Array.from(set)
        if (this.historyList.length >= 10) {
          this.historyList.shift()
        }
        uni.setStorageSync('kw', JSON.stringify(this.historyList))
      },
      clean() {
        this.historyList = []
        uni.setStorageSync('kw', '[]')
      },
      gotoGoodsList(e) {
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?query=' + e
        })
      }
    },
    computed: {
      histories() {
        return [...this.historyList].reverse()
      }
    },
    onLoad() {
      this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
    }
  }
</script>

<style lang="scss">
  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
  }

  .sugg-list {
    padding: 0 10px;

    .sugg-item {
      display: flex;
      align-items: center;
      justify-content: space-between;
      font-size: 12px;
      padding: 10px 0;
      border-bottom: 1px solid #efefef;

      .goods-name {
        // 文字不允许换行（单行文本）
        white-space: nowrap;
        // 溢出部分隐藏
        overflow: hidden;
        // 文本溢出后，使用 ... 代替
        text-overflow: ellipsis;
      }
    }
  }

  .history-box {
    padding: 0 5px;

    .history-title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-size: 13px;
      border-bottom: 1px solid #efefef;
      height: 40px;
    }

    .history-list {
      display: flex;
      flex-wrap: wrap;

      .uni-tag {
        margin-top: 8px;
        margin-right: 8px;
      }
    }
  }
</style>